package analyses;

import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class SunLightPanel extends JPanel implements ChangeListener {
	JSpinner longitudeSpinner;
	JSpinner latitudeSpinner;
	JSpinner monthSpinner;
	JSpinner daySpinner;
	JSpinner yearSpinner;
	JSpinner hourSpinner;
	JSpinner minuteSpinner;
	JLabel elevationLabel;
	JLabel azimutLabel;
	
	double azimut;
	double elevation;
	double vX;
	double vY;
	
public SunLightPanel(){
	
	GridLayout layout=new GridLayout(4,0);
	this.setLayout(layout);
	
	
	SpinnerModel model;
	
	JPanel locationPanel=this.getGroupPanel("Location");
	this.add(locationPanel);
	model=new SpinnerNumberModel( 48.1,-90,90,0.1);
	latitudeSpinner=getSpinner("Lat",model);
	locationPanel.add(latitudeSpinner);
	
	model=new SpinnerNumberModel(11.6,-180,180,0.1);
	longitudeSpinner=getSpinner("Lon",model);
	locationPanel.add(longitudeSpinner);
	
	JPanel datePanel=this.getGroupPanel("Date");
	this.add(datePanel);
	
	model=new SpinnerNumberModel(1,1,31,1);  
	daySpinner=getSpinner("Day",model);
	datePanel.add(daySpinner);
	
	model=new SpinnerNumberModel(6,1,12,1);  
	monthSpinner=getSpinner("Month",model);
	datePanel.add(monthSpinner);
	
	model=new SpinnerNumberModel(2000,2000,2020,1);  
	yearSpinner=getSpinner("Year",model);
	datePanel.add(yearSpinner);
	
	
	JPanel timePanel=this.getGroupPanel("Time");
	this.add(timePanel);
	model=new SpinnerNumberModel(10,1,24,1);  
	hourSpinner=getSpinner("Hour",model);
	timePanel.add(hourSpinner);
	model=new SpinnerNumberModel(0,0,60,1);  
	minuteSpinner=getSpinner("Minute",model);
	timePanel.add(minuteSpinner);
	
	JPanel sunPanel=this.getGroupPanel("Sun-Angle");
	this.add(sunPanel);
	azimutLabel=new JLabel();
	
	addTitledBorder(azimutLabel,"Azimut");
	sunPanel.add(azimutLabel);
	elevationLabel=new JLabel();
	addTitledBorder(elevationLabel,"Elevation");
	sunPanel.add(elevationLabel);
	
	
	calculateSun();
}

public JPanel getGroupPanel(String name){
	JPanel panel=new JPanel();
	addTitledBorder(panel,name);
	GridLayout gridLayout = new GridLayout(0,3);
	panel.setLayout(gridLayout);
	return panel;
}

public void addTitledBorder(JComponent component,String name){
	Border border=BorderFactory.createTitledBorder(name);
	component.setBorder(border);
}

public JSpinner getSpinner(String name,SpinnerModel model){
	JSpinner jSpinner=new JSpinner(model);
	jSpinner.addChangeListener(this);
	addTitledBorder(jSpinner,name);
return jSpinner;
}
public void calculateSun(){
	double longitude=(Double) this.longitudeSpinner.getValue();
	double latitude=(Double) this.latitudeSpinner.getValue();
	int year=(Integer)yearSpinner.getValue();
	int month=(Integer)monthSpinner.getValue();
	int day=(Integer)daySpinner.getValue();
	int hour=(Integer)hourSpinner.getValue();
	int min=(Integer)minuteSpinner.getValue();
	SunLightCalculator sL=new SunLightCalculator(longitude,latitude);
	double[] aE=sL.getAzimutElevation(year, month, day, hour, min, 0);
	azimut=aE[0];
	elevation=aE[1];
	azimutLabel.setText(""+(Math.round(azimut*100.)/100.));
	elevationLabel.setText(""+(Math.round(elevation*100.)/100.));
	double[] vec=sL.getShadowVector(aE);
	vX=vec[0];
	vY=vec[1];
}



public double[][] getStudy(){
	double[][] suns=new double[24][2];
	double longitude=(Double) this.longitudeSpinner.getValue();
	double latitude=(Double) this.latitudeSpinner.getValue();
	int year=(Integer)yearSpinner.getValue();
	int month=(Integer)monthSpinner.getValue();
	int day=(Integer)daySpinner.getValue();
	for (int i=0;i<24;i++){
		SunLightCalculator sL=new SunLightCalculator(longitude,latitude);
		double[] aE=sL.getAzimutElevation(year, month, day, i, 0, 0);
		suns[i]=aE;
	}
	
	return suns;
}
public double[][] getStudyVec(){
	double[][] suns=getStudy();
	double[][] v=new double[suns.length][2];
	for (int i=0;i<suns.length;i++){
		double[]cV=SunLightCalculator.getShadowVector(suns[i]);
		v[i][0]=cV[0];
		v[i][1]=cV[1];
	}
	
	return v;
}

public double[] getVec(int hour){
	
	double longitude=(Double) this.longitudeSpinner.getValue();
	double latitude=(Double) this.latitudeSpinner.getValue();
	int year=(Integer)yearSpinner.getValue();
	int month=(Integer)monthSpinner.getValue();
	int day=(Integer)daySpinner.getValue();
	
		SunLightCalculator sL=new SunLightCalculator(longitude,latitude);
		double[] aE=sL.getAzimutElevation(year, month, day, hour, 0, 0);
		double[] v=SunLightCalculator.getShadowVector(aE);

	
	return v;
}

public void stateChanged(ChangeEvent arg0) {
	// TODO Auto-generated method stub
	
	calculateSun();
	
}

public double getAzimut() {
	return azimut;
}

public double getElevation() {
	return elevation;
}

public double getVX() {
	return vX;
}

public double getVY() {
	return vY;
}

}
